<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2018/12/5
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<style>
    .select2-selection{
        min-width: 120px;
    }
</style>
<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 审批列表
            </div>
            <div class="box-tools pull-right">
                <sec:authorize
                        access="hasAnyRole({'ROLE_ENTERPRISE_MAINTAINING_MAINTAIN_AUDIT_ORDER'})">
                    <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                    <a href="javascript:;" onclick="audit(0)"
                       class="btn btn-default btnTool" title="审核通过"><i class="fa fa-plus"></i> 审核通过</a>
                    <a href="javascript:;" onclick="audit(-1)"
                       class="btn btn-default btnTool" title="审核驳回"><i class="fa fa-plus"></i> 审核驳回</a>
                </sec:authorize>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/maintainOrder/auditListJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">工单名称：</label>
                    <div class="control-inline">
                        <input type="text" id="searchName" name="searchName" value="" maxlength="64"
                               class="form-control width-120">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单编号：</label>
                    <div class="control-inline">
                        <input type="text" id="code" name="code" value="" maxlength="64"
                               class="form-control width-120">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单类型：</label>
                    <div class="control-inline">
                        <select id="searchType" name="searchType" class="form-control " >
                            <option value="" selected>&nbsp;&nbsp;&nbsp;全部工单&nbsp;&nbsp;&nbsp;</option>
                            <option value="false">&nbsp;周期性工单&nbsp;</option>
                            <option value="true">&nbsp;一次性工单&nbsp;</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">审核类型：</label>
                    <div class="control-inline">
                        <select id="searchAudit" name="searchAudit" class="form-control " >
                            <option value="">&nbsp;&nbsp;&nbsp;全部&nbsp;&nbsp;&nbsp;</option>
                            <option value="delay">延期审核</option>
                            <option value="cancel">取消审核</option>
                            <option value="complete">完成审核</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">开始时间：</label>
                    <div class="control-inline">
                        <input type="text" id="startTime" name="start" value="" maxlength="64"
                               class="form-control Wdate " style="width: 200px" autoComplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">结束时间：</label>
                    <div class="control-inline">
                        <input type="text" id="endTime" name="end" value="" maxlength="64"
                               class="form-control Wdate " style="width: 200px" autoComplete="off">
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>
            <div style="overflow-x:auto">
                <table id="dataGrid"></table>
            </div>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<script>
    $('#dataGrid').dataGrid({
        searchForm: $("#searchForm"),
        showCheckbox: true,
        shrinkToFit: false,
        autoScroll: true,
        columnModel: [
            {header: '工单号', name: 'orderCode', index: 'orderCode', width: 200, align: "center",sortable: false},
            {header: '工单名', name: 'orderName', index: 'orderName', width: 150, align: "center",sortable: false},
            {header: '审批类型', name: 'auditType', index: 'auditType', width: 120, align: "center",sortable: false},
            {header: '工单类型', name: 'orderType', index: 'orderType', width: 100, align: "center",sortable: false},
            {header: '当前审批等级', name: 'level', index: 'level', width: 120, align: "center",sortable: false},
            {header: '申请原因', name: 'reason', index: 'reason', width: 150, align: "center",sortable: false},
            {header: '工单状态', name: 'orderStatus', index: 'orderStatus', width: 120, align: "center",sortable: false},
            {header: '检查结果', name: 'abnormal', index: 'abnormal', width: 120, align: "center",sortable: false},
            {header: '是否终极审核', name: 'auditLast', index: 'auditLast', width: 120, align: "center",sortable: false},
            {header: '接单人', name: 'receiveUserName', index: 'receiveUserName', width: 120, align: "center",sortable: false},
            {header: '开始时间', name: 'startDateTime', index: 'startDateTime', width: 150, align: "center",sortable: false},
            {header: '结束时间', name: 'endDateTime', index: 'endDateTime', width: 150, align: "center",sortable: false},

        ],
        ajaxSuccess: function (data) {

        },
    });

    function audit(status) {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要审核的工单");
            return false;
        }
        var flag = 1;
        var type = "";
        var audit = "";
        if (status==0) {
            $.each(ids, function (i, item) {
                var ret = $("#dataGrid").jqGrid('getRowData', item);
                var abnormal = ret.abnormal;
                var auditType = ret.auditType;
                var auditLast = ret.auditLast;
                if (abnormal == "异常") {
                    flag = 2;
                }
                if (type == "") {
                    type = auditType;
                }
                if (type != "" && type != auditType) {
                    flag = 3;
                }
                if (audit == "") {
                    audit = auditLast;
                }
                if (audit != "" && audit != auditLast) {
                    flag = 4;
                }
                if (auditType != "完成审批") {
                    // alert(auditType)
                    flag = 5;
                }
            });
            if (flag == 2 && ids.length > 1) {
                js.showMessage("存在检查结果为异常的工单，无法批量给审核");
                return false;
            } else if (flag == 3 && ids.length > 1) {
                js.showMessage("存在不同审批操作的工单，无法批量给审核");
                return false;
            } else if (flag == 4 && ids.length > 1) {
                js.showMessage("同时存在终极审核和非终极审核的工单，无法批量审核");
                return false;
            } else if (flag == 5 && ids.length > 1) {
                js.showMessage("只有完成审批操作才能批量审批");
                return false;
            }
            js.confirm('确认审批通过？', function () {
                if (audit == "是") {
                    vLayuiUtil.openLayerIframWindow('wJudge', '<c:url value="/web/admin/maintainOrder/judgeOrder.action?id="/>' + ids, '审核工单',['50%','50%'])

                } else {
                    $.ajax({
                        type: 'post',
                        url: "<c:url value="/web/admin/maintainOrder/auditOrder.action" />",
                        data: {"ids": JSON.stringify(ids), "type": status},
                        dataType: 'json',
                        async: false,
                        success: function (data) {
                            js.showMessage(data.msg);
                            $('#dataGrid').dataGrid('refresh');
                        },
                        error: function () {
                            js.showMessage("出错了，请联系管理员");
                        }
                    });
                }

            })

        } else if (status == -1) {

            if (ids.length > 1) {
                js.showMessage("该操作无法批量审批");
                return false;
            }
            vLayuiUtil.openLayerIframWindow('wAudit', '<c:url value="/web/admin/maintainOrder/auditOrderEdit.action?id="/>' + ids + "&&type=" + status, '审核工单',['50%','50%'])


        }
    }

    var date = new Date();
    $("#startTime").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language:'zh-CN',
        minuteStep:1,
        autoclose:true,//选中关闭
        todayBtn: true//今日按钮
    });
    $("#endTime").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language:'zh-CN',
        minuteStep:1,
        autoclose:true,//选中关闭
        todayBtn: true//今日按钮
    });

    // 时间选择器互动（结束时间不得早于开始时间）
    $("#startTime").on('changeDate', function(ev) {
        $("#endTime").datetimepicker('setStartDate', ev.date);
        var d = $("#endTime").val();
        if (d) {
            var date = new Date(d.replace(/-/g, '/'));
            if (date != 'Invalid Date' && date < ev.date) {
                $("#endTime").datetimepicker('setDate', ev.date)
            }
        }
    });


</script>
